package com.nine.mymall.search.controller;

import com.nine.mymall.search.service.MallSearchService;
import com.nine.mymall.search.vo.SearchParam;
import com.nine.mymall.search.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 封装页面所有可能传入的参数
 * @author: duonine
 * @date: 2021/11/10
 */
@Controller
public class SearchController {

    @Autowired
    MallSearchService mallSearchService;

    /**
     * 自动将页面提交过来的所有请求参数封装
     * @param param
     * @return
     */
    @GetMapping("/list.html")
    public String listPage(SearchParam param, Model model, HttpServletRequest request){

        // 拿到原生的url参数
        String queryString = request.getQueryString();
        param.set_queryString(queryString);

        // 跟进页面传递来的参数，去es中检索商品
        SearchResult result = mallSearchService.search(param);
        model.addAttribute("result", result);
        return "list";
    }

}
